package com.ebrothers.isns.activity;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

public abstract class SplashScreen extends Activity {
	private long mThreadSleepTime = 0;
	private int mBackgroundImage = -1;
	private boolean mProgressBarVisble = true;
	private boolean mIsRunning = true;
	private boolean mCancelable = true;
	private boolean mAutoCompletable = true;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Window w = getWindow();
		if (-1 != mBackgroundImage) {
			w.setBackgroundDrawableResource(mBackgroundImage);
		}

		if (mProgressBarVisble) {
			// generate a new linear layout, and add it into root context
			LinearLayout topLayout = new LinearLayout(this);
			LinearLayout.LayoutParams topLayoutParams = new LinearLayout.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
			w.addContentView(topLayout, topLayoutParams);

			// generate a new relative layout, and add it into top linear layout
			RelativeLayout childLayout = new RelativeLayout(this);
			RelativeLayout.LayoutParams childLayoutParams = new RelativeLayout.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
			childLayout.setLayoutParams(childLayoutParams);
			topLayout.addView(childLayout);

			// generate a new progress bar, and add it into relative layout
			ProgressBar progress = new ProgressBar(this);
			RelativeLayout.LayoutParams progressLayoutParams = new RelativeLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			// set the rule: showing the process bar in center of its parent
			progressLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,
					RelativeLayout.TRUE);
			progress.setLayoutParams(progressLayoutParams);
			childLayout.addView(progress);
		}

		// start another thread to do the job(e.g. request server)
		// when the job is done, then this welcome activity will be closed
		new Thread() {
			@Override
			public void run() {
				Bundle bundle = runBackground();
				try {
					Thread.sleep(mThreadSleepTime);
				} catch (InterruptedException e) {
				}
				if (mIsRunning && mAutoCompletable) {
					doneJob(bundle);
				}
			}
		}.start();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (KeyEvent.KEYCODE_BACK == keyCode) {
			if (mCancelable) {
				mIsRunning = false;
				onCanceled();
			}
			return mIsRunning;
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * Doing the job (e.g. request server) on background, after this, will call
	 * {@link SplashScreen#onJobDone(Bundle)}.
	 * 
	 * @return the result
	 */
	abstract protected Bundle runBackground();

	/**
	 * After doing the job (e.g. request server) which running on
	 * background(call {@link SplashScreen#runBackground}), this method will be
	 * called to do the ending job.
	 * 
	 * @param bundle
	 *            the result of running job on background
	 */
	abstract protected void onJobDone(Bundle bundle);

	/**
	 * Manually done the job with data.
	 */
	public void doneJob(Bundle bundle) {
		onJobDone(bundle);
		SplashScreen.this.finish();
	}

	/**
	 * You can override this method to do the job after canceling.
	 */
	protected void onCanceled() {
		finish();
	}

	protected void setRunBackgroundDelay(long time) {
		mThreadSleepTime = time;
	}

	protected void setBackgroundImage(int rid) {
		mBackgroundImage = rid;
	}

	protected void showProgressBar(boolean value) {
		mProgressBarVisble = value;
	}

	/**
	 * @return the cancelable
	 */
	public boolean isCancelable() {
		return mCancelable;
	}

	/**
	 * @param cancelable
	 *            the cancelable to set
	 */
	public void setCancelable(boolean cancelable) {
		mCancelable = cancelable;
	}

	/**
	 * @return the isRunning
	 */
	public boolean isRunning() {
		return mIsRunning;
	}

	public void setAutoCompletable(boolean flag) {
		mAutoCompletable = flag;
	}

	protected void forceFullScreen() {
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().clearFlags(
				WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
	}

	protected void forceNoTitleBar() {
		getWindow().requestFeature(android.view.Window.FEATURE_NO_TITLE);
	}

	protected void forceLanscapte() {
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	}

	protected void forcePortrait() {
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}
}